package initialize

import (
	"gitee.com/DonHz/electronic-business-systems/mxshop_srvs/inventory_srv/global"
	"gitee.com/DonHz/electronic-business-systems/mxshop_srvs/inventory_srv/model"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"gorm.io/gorm/logger"
	"log"
	"os"
	"time"
)

func InitDB() {
	dsn := "root:123456@tcp(127.0.0.1:3306)/mxshop_inventory_srv?charset=utf8mb4&parseTime=true"
	newLogger := logger.New(
		log.New(os.Stdout, "\r\n", log.LstdFlags),
		logger.Config{
			SlowThreshold: time.Second, //慢阈值
			LogLevel:      logger.Info,
			Colorful:      true,
		},
	)
	var err error
	global.DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{
		Logger: newLogger,
	})
	if err != nil {
		panic(err)
	}
	_ = global.DB.AutoMigrate(&model.Inventory{})
}
